﻿/// <reference path="../Jquery/jquery-1.6.2.min.js" />
/// <reference path="../Jquery/jquery-1.4.4-vsdoc.js" />

var PageCategorySelector = function (containerId) {
    if (containerId == undefined) {
        alert("Coding...");
    }
    this.ContainerId = containerId;
}

PageCategorySelector.prototype.GetInstance = function () {
    var selector = this;
    //selector = new PageCategorySelector();
    return selector;
}


PageCategorySelector.prototype.Init = function () {
    var selector = this.GetInstance();
    $.getJSON("/PageCategory/List/", function (data) {
        $("#" + selector.ContainerId).append(selector.CreateSelect(data));
    });
}

PageCategorySelector.prototype.CreateSelect = function (categories) {
    var selector = this.GetInstance();
    var select = $("<select></select>");
    select.append("<option>Null</option>")
    for (var i = 0; i < categories.length; i++) {
        var option = $("<option value='" + categories[i].ID + "'>" + categories[i].DisplayText + "</option>");
        option.data("data", categories[i]);
        select.append(option);
    }
    select.change(function () {
        $(this).nextAll("select").remove();
        var option = $(this).children(":selected");
        var category = option.data("data");
        if (category != undefined && category.Children.length > 0) {
            $(this).after(selector.CreateSelect(category.Children));
        }
    });
    return select;
}
